package com.zoo.boot.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zoo.boot.api.CommonResult;
import com.zoo.boot.api.PagingDto;
import com.zoo.boot.bean.RoleEntity;
import com.zoo.boot.model.LRole;
import com.zoo.boot.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 角色管理 Controller
 * @Author: WangB
 * @Date: 2021/6/29
 */
@Controller
@Api(tags = "SysRoleController", value = "角色管理")
@RequestMapping("/sys/role")
public class SysRoleController {


    @Autowired
    private RoleService roleService;


    @GetMapping("/index")
    @ApiOperation("展示角色列表")
    public  String index(){
        return  "system/role/role";
    }


    @GetMapping("/list")
    @ResponseBody
    @ApiOperation("读取角色列表")
    public CommonResult  list(PagingDto dto, LRole role){
        Page<Object> objects = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<LRole> roles = roleService.getRoles(role);
        return  CommonResult.tableSuccess(roles).setCount(objects.getTotal());
    }

    @GetMapping("/add")
    @ApiOperation("展示角色新增页面")
    public String navAddPage(Map map){
        map.put("roleInfo",new LRole());
        return  "system/role/role-add";
    }


    @PostMapping
    @ResponseBody
    @ApiOperation("添加角色信息")
    public CommonResult  add(@RequestBody RoleEntity  roleEntity){
        if(roleService.insertRole(roleEntity)){
            return  CommonResult.success();
        }
        return  CommonResult.fail();
    }


    @GetMapping("/edit")
    @ApiOperation("展示修改页面")
    public String  navUpdatePage(Integer roleId, Model model){
        model.addAttribute("roleInfo",roleService.getRoleByRoleId(roleId));
        return  "system/role/role-edit";
    }


    @PutMapping
    @ResponseBody
    @ApiOperation("编辑菜单数据")
    public  CommonResult  update(@RequestBody RoleEntity info){
        if(roleService.update(info)){
            return  CommonResult.success();
        }
        return  CommonResult.fail();
    }


    @DeleteMapping
    @ResponseBody
    @ApiOperation("根据roleId删除角色")
    public CommonResult delete(Integer roleId){
        if(roleService.delete(roleId)){
            return  CommonResult.success();
        }
        return  CommonResult.fail();
    }
}
